﻿using System.Text;

namespace Language.Utility
{
    public static class Hash
    {
        public const ulong FNV64_PRIME = 0x100000001B3;
        public const ulong FNV64_OFFSET = 0xCBF29CE484222325;

        /// <summary>
        /// This function generates an FNV64 hash of the supplied data.
        /// </summary>
        /// <remarks>The string data is converted to ASCII first, before the hash is calculated
        /// https://en.wikipedia.org/wiki/Fowler–Noll–Vo_hash_function</remarks>
        public static ulong Fnv64( string data, Encoding encoding, ulong hash = FNV64_OFFSET )
        {
            byte[] byteData = encoding.GetBytes( data );

            foreach( byte b in byteData )
            {
                hash = ( hash ^ b ) * FNV64_PRIME;
            }

            return hash;
        }

    }
}
